---
title: Replication for Group Consciousness, Organizational Membership and District
  Choice
author: "Mateo Villamizar Chaparro"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=FALSE}
# Load the needed packages
knitr::opts_chunk$set(echo = TRUE)
rm(list=ls())
library(cregg)
library(dplyr)
library("tidyr")
library(xtable)
library(stargazer)
library(lmtest)
library(sandwich)
library(ggplot2)
```

```{r data}
# Set-up working directory
setwd("")

# Set the different folders for exporting, make sure your directory has two folders one for tables and one for images
images <- c("")
tables <- c("")

# Upload the datasets
afro <- read.csv("afro_replic.csv", stringsAsFactors = FALSE)
regs <- read.csv("distrito_afro_regs.csv", stringsAsFactors = FALSE)

## generating a placeholder matrix
camce <- matrix(NA,10,3)
colnames(camce) <- c("CAMCE difference","MM difference - afro","MM difference - territ")
rownames(camce) <- c("Historical Discrimination", "", "Linked Fate", "", "Afro treated worse", "", "Ethnic Orgs", "", "Community Councils", "")
```

```{r factors}
# Features are imported as strings and we need to make them factors for the conjoint analysis
# making the conjoint features into factors
afro$FeatCirc[afro$FeatCirc == 0] <- 'Territorial'
afro$FeatCirc[afro$FeatCirc == 1] <- 'Afro'
afro$FeatNumcand[afro$FeatNumcand == 1] <- "Less than 5"
afro$FeatNumcand[afro$FeatNumcand == 2] <- "Between 5 and 14"
afro$FeatNumcand[afro$FeatNumcand == 3] <- "More than 14"
afro$FeatExp[afro$FeatExp == 1] <- "No Exp Execution"
afro$FeatExp[afro$FeatExp == 2] <- "Exp Execution"
afro$FeatExp[afro$FeatExp == 3] <- "No Exp Politics"
afro$FeatExp[afro$FeatExp == 4] <- "Experience Politics"
afro$FeatClient[afro$FeatClient == 1] <- "Clientelistic"
afro$FeatClient[afro$FeatClient == 2] <- "Not Clientelistic"
afro$FeatEth[afro$FeatEth == 1] <- "Majority Black"
afro$FeatEth[afro$FeatEth == 2] <- "Majority White"
afro$FeatLegis[afro$FeatLegis == 1] <- "No projects"
afro$FeatLegis[afro$FeatLegis == 2] <- "Some projects"
afro$FeatLegis[afro$FeatLegis == 3] <- "A lot of projects"
afro$FeatFam[afro$FeatFam == 1] <- "Community"
afro$FeatFam[afro$FeatFam == 2] <- "Family"
afro$FeatFam[afro$FeatFam == 3] <- "No community or family"

# Defining reference levels for each attribute
afro$FeatCirc <- as.factor(afro$FeatCirc)
afro <- within(afro, FeatCirc <- relevel(FeatCirc, ref = "Territorial"))
afro$FeatNumcand <- as.factor(afro$FeatNumcand)
afro <- within(afro, FeatNumcand <- relevel(FeatNumcand, ref = "Less than 5"))
afro$FeatExp <- as.factor(afro$FeatExp)
afro <- within(afro, FeatExp <- relevel(FeatExp, ref = "No Exp Execution"))
afro$FeatClient <- as.factor(afro$FeatClient)
afro$FeatEth <- as.factor(afro$FeatEth)
afro$FeatLegis <- as.factor(afro$FeatLegis)
afro <- within(afro, FeatLegis <- relevel(FeatLegis, ref = "No projects"))
afro$FeatFam <- as.factor(afro$FeatFam)
afro <- within(afro, FeatFam <- relevel(FeatFam, ref = "No community or family"))

## Naming the attributes
attr(afro$FeatCirc, "label") <- "Type of District"
attr(afro$FeatNumcand, "label") <- "Number of Candidates"
attr(afro$FeatExp, "label") <- "Experience"
attr(afro$FeatClient, "label") <- "Vote Buying"
attr(afro$FeatEth, "label") <- "Ethnicity"
attr(afro$FeatLegis, "label") <- "Legilsative Activity"
attr(afro$FeatFam, "label") <- "Who votes?"

## Making the sub-group variables factors as well
afro$blvswt <- factor(afro$blvswt)
afro$racdisc <- factor(afro$racdisc)
afro$histdis <- factor(afro$histdis)
afro$linkfate <- factor(afro$linkfate)
afro$selfid <- factor(afro$selfid)
afro$ethorg <- factor(afro$ethorg)
afro$conscom <- factor(afro$conscom)

# Creating the robustness datasets
# Attention checks
att_check <- afro %>% filter(D2=="Circunscripciones")
# Concentration of the respodnents
concent <- afro %>% filter(H3!="Demasiadas")
```


## MAIN PAPER TABLES AND FIGURES

# Figure 1a and 1b

```{r fig1, warning=FALSE}
# Fig 1a
parties <- as.data.frame(cbind(c("2006-2010",	"2010-2014",	"2014-2018",	"2018-2022",	"2022-2026"),c(27,	66,	28,	42,	48)))
colnames(parties) <- c("years","parties")
parties$parties <- as.numeric(parties$parties)

# export the plot
png(file=paste0(images,"/party_orgs.png"), width = 1100)
ggplot(parties, aes(x=years, y=parties)) + geom_bar(stat = "identity") +
  geom_hline(yintercept=mean(parties$parties)) + 
  xlab("Congressional Period") + 
  ylab("Number of Organizations \n running for elections") +
  theme(text=element_text(size=30))
dev.off()

# Fig 1b
candidates <- as.data.frame(cbind(c("2006-2010",	"2010-2014",	"2014-2018",	"2018-2022",	"2022-2026"),c(48,	168,	69,	109,	127)))
colnames(candidates) <- c("years","candidates")
candidates$candidates <- as.numeric(candidates$candidates)

# export the plot
png(file=paste0(images,"/candidates.png"), width = 1100)
ggplot(candidates, aes(x=years, y=candidates)) + geom_bar(stat = "identity") +
  geom_hline(yintercept=mean(candidates$candidates)) + 
  xlab("Congressional Period") + 
  ylab("Number of Candidates \n running for elections") +
  theme(text=element_text(size=30))
dev.off()
```

# Figure 2

```{r fig2, warning=FALSE}
##### Main formula for the conjoint
f1 <- chosen_you ~ FeatCirc + FeatNumcand + FeatExp + FeatClient + FeatEth + FeatLegis + FeatFam

#########################################################################################
### Historical Discrimination
# Difference in marginal means
diff_mms <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~histdis)

# Difference in AMCEs
diff_amces_2 <- cj(na.omit(afro), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~histdis, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_histdiscr.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

# CAMCE Tables
camce[1,1] <- diff_amces_2$estimate
camce[2,1] <- diff_amces_2$std.error
camce[1,2] <- diff_mms$estimate[2]
camce[2,2] <- diff_mms$std.error[2]
camce[1,3] <- diff_mms$estimate[1]
camce[2,3] <- diff_mms$std.error[1]

#########################################################################################
### Linked Fate
# Difference in marginal means
diff_mms <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~linkfate)

# Difference AMCEs
diff_amces_2 <- cj(na.omit(afro), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~linkfate, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_linkedfate.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

# extract the estimates
camce[3,1] <- diff_amces_2$estimate
camce[4,1] <- diff_amces_2$std.error
camce[3,2] <- diff_mms$estimate[2]
camce[4,2] <- diff_mms$std.error[2]
camce[3,3] <- diff_mms$estimate[1]
camce[4,3] <- diff_mms$std.error[1]


#########################################################################################
### Afro treated worst
# Difference in marginal means
diff_mms <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~blvswt)

# Difference AMCEs
diff_amces_2 <- cj(na.omit(afro), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~blvswt, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_afroworse.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

# extract the estimates
camce[5,1] <- diff_amces_2$estimate
camce[6,1] <- diff_amces_2$std.error
camce[5,2] <- diff_mms$estimate[2]
camce[6,2] <- diff_mms$std.error[2]
camce[5,3] <- diff_mms$estimate[1]
camce[6,3] <- diff_mms$std.error[1]
```
# Figure 3

```{r fig3, warning=FALSE}
##### Main formula for the conjoint
f1 <- chosen_you ~ FeatCirc + FeatNumcand + FeatExp + FeatClient + FeatEth + FeatLegis + FeatFam

#########################################################################################
### Ethnic Organizations
# Difference in Marginal Means
diff_mms <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~ethorg)

# Difference in AMCEs
diff_amces_2 <- cj(na.omit(afro), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~ethorg, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_ethorg.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

# extract the estimates
camce[7,1] <- diff_amces_2$estimate
camce[8,1] <- diff_amces_2$std.error
camce[7,2] <- diff_mms$estimate[2]
camce[8,2] <- diff_mms$std.error[2]
camce[7,3] <- diff_mms$estimate[1]
camce[8,3] <- diff_mms$std.error[1]

#########################################################################################
### Community Council
# Difference in Marginal Means
diff_mms <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~conscom)

# Difference in AMCEs
diff_amces_2 <- cj(na.omit(afro), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~conscom, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_conscom.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

# extract the estimates
camce[9,1] <- diff_amces_2$estimate
camce[10,1] <- diff_amces_2$std.error
camce[9,2] <- diff_mms$estimate[2]
camce[10,2] <- diff_mms$std.error[2]
camce[9,3] <- diff_mms$estimate[1]
camce[10,3] <- diff_mms$std.error[1]

```
# Table 2

```{r table2, warning=FALSE}
regs$ParticipantRacialDiscrim <- factor(regs$ParticipantRacialDiscrim)

# Within people who are in Ethnic Organizations
eote1 <- lm(territ2018 ~ dist_reps_bad + edad + female + hs + married + income + linkfate + as.factor(departamento), data = regs %>% filter(ethorg==1))
eote1r <- coeftest(eote1, vcov = vcovHC(eote1, type="HC1"))
eote2 <- lm(territ2018 ~ conf_orgs + edad + female + hs + married + income + linkfate + as.factor(departamento), data = regs %>% filter(ethorg==1))
eote2r <- coeftest(eote2, vcov = vcovHC(eote2, type="HC1"))
eote3 <- lm(territ2018 ~ conf_cands + edad + female + hs + married + income + linkfate + as.factor(departamento), data = regs %>% filter(ethorg==1))
eote3r <- coeftest(eote3, vcov = vcovHC(eote3, type="HC1"))

# Within people who are in Community Councils
eote4 <- lm(territ2018 ~ dist_reps_bad + edad + female + hs + married + income + linkfate + as.factor(departamento), data = regs %>% filter(conscom==1))
eote4r <- coeftest(eote4, vcov = vcovHC(eote4, type="HC1"))
eote5 <- lm(territ2018 ~ conf_orgs + edad + female + hs + married + income + linkfate + as.factor(departamento), data = regs %>% filter(conscom==1))
eote5r <- coeftest(eote5, vcov = vcovHC(eote5, type="HC1"))
eote6 <- lm(territ2018 ~ conf_cands + edad + female + hs + married + income + linkfate + as.factor(departamento), data = regs %>% filter(conscom==1))
eote6r <- coeftest(eote6, vcov = vcovHC(eote6, type="HC1"))

# Exporting the models
stargazer(eote1r, eote2r, eote3r, eote4r, eote5r, eote6r,
          keep=c("territ2018","conf_cands", "dist_reps_bad", "conf_orgs", "edad", "female", "hs", "married", "income", "linkfate"),
          add.lines = list(c("Dept. FE", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Num. Obs", nobs(eote1), nobs(eote2), nobs(eote3), nobs(eote4), nobs(eote5), nobs(eote6)),
                           c("R Squared", round(summary(eote1)$adj.r.squared,3), round(summary(eote2)$adj.r.squared,3), round( summary(eote3)$adj.r.squared,3), round(summary(eote4)$adj.r.squared,3), round( summary(eote5)$adj.r.squared,3), round(summary(eote6)$adj.r.squared,3))),
          covariate.labels=c("Bad District Reps", "Trust Ethnic Orgs", "Trust Ethnic Reps", "Age", "Female", "Finished high school", "Married", "Income", "Linked Fate"),
          dep.var.caption = c("Voted in Territorial District (2018)"),
          #out=paste0(tables,"/ethorg_strat.tex"),
          type= "text"
          )
```


## APPENDIX TABLES AND FIGURES

# Table A2
```{r tabA2, warning=FALSE}
desc_stats <- regs %>% dplyr::select(edad, married, hs, female, income, conscom, ethorg, histdis, blvswt, racdisc, linkfate, selfid, dist_reps_bad, conf_orgs, conf_cands)

# Exporting the table
stargazer(desc_stats, digits = 2,
          covariate.labels=c("Age", "Married", "Finished high school", "Female", "Income", "Member Community Council", "Member Ethnic Organization", "Historical Discrimination", "Black people treated worse than Whites", "Suffered racial discrimination", "Linked Fate measure", "Self-Identification is important", "Bad District Reps", "Trust Ethnic Orgs", "Trust Ethnic Reps"),
          omit.summary.stat = c("p25", "p75"),
          #out=paste0(tables,"/descstats.tex"),
          type= "text")
```

## Table A3
```{r tabA3, warning=FALSE}
# Some regressions explaining the probability to vote in the ethnic district
re1 <- lm(voted2018 ~ histdis + edad + female + hs + married + income + as.factor(departamento), data = regs)
re1r <- coeftest(re1, vcov = vcovHC(re1, type="HC1"))
re2 <- lm(voted2018 ~ linkfate + edad + female + hs + married + income + as.factor(departamento), data = regs)
re2r <- coeftest(re2, vcov = vcovHC(re2, type="HC1"))
re3 <- lm(voted2018 ~ blvswt + edad + female + hs + married + income + as.factor(departamento), data = regs)
re3r <- coeftest(re3, vcov = vcovHC(re3, type="HC1"))
re4 <- lm(voted2018 ~ ethorg + edad + female + hs + married + income + as.factor(departamento), data = regs)
re4r <- coeftest(re4, vcov = vcovHC(re4, type="HC1"))
re5 <- lm(voted2018 ~ conscom + edad + female + hs + married + income + as.factor(departamento), data = regs)
re5r <- coeftest(re5, vcov = vcovHC(re5, type="HC1"))
re6 <- lm(voted2018 ~ selfid + edad + female + hs + married + income + as.factor(departamento), data = regs)
re6r <- coeftest(re6, vcov = vcovHC(re6, type="HC1"))

stargazer(re1r, re2r, re3r, re4r, re5r,re6r,
          type="text", 
          keep=c("voted2018","histdis","linkfate","blvswt","ethorg","conscom","selfid","edad","female","hs","married","income","dist_reps_bad","conf_orgs"),
          add.lines = list(c("Dept. FE", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Num. Obs", nobs(re1), nobs(re2), nobs(re3), nobs(re4), nobs(re5), nobs(re6),
                           c("R Squared", round(summary(re1)$adj.r.squared,3)), round(summary(re2)$adj.r.squared,3), round( summary(re3)$adj.r.squared,3), round(summary(re4)$adj.r.squared,3), round( summary(re5)$adj.r.squared,3), round(summary(re6)$adj.r.squared,3))),
          covariate.labels=c("Historical Discrimination", "Linked Fate", "Afro treated Worse", "Ethnic Organization", "Community council", "Self identification", "Age", "Female", "Finished high school", "Married", "Income"),
          dep.var.caption = c("Voted in Ethnic District (2018)")
          #out=paste0(tables,"/regs.tex")
          )
```
# Table A4
```{r tabA4}
xtable(round(camce, 4))
```
# Figures A1 AND A2
```{r figA1_A2, warning=FALSE}
# Plot for the conjoint's AMCEs
amces <- cj(afro, f1, id = ~id, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))
png(file=paste0(images,"/amce_total.png"), width = 600)
plot(amces)
dev.off()

# Plot for the conjoint's Marginal Means
png(file=paste0(images,"/mm_total.png"), width = 600)
plot(mm(afro, f1, id = ~id), vline = 0.5)
dev.off()
```

# Figures A3 to A23
```{r figsA3_A23, warning=FALSE}
# Figure A3: AMCEs for for Historical discrimination all attributes
amces <- cj(na.omit(afro), f1, id = ~id, estimate = "amce", by = ~histdis, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))

png(file=paste0(images,"/amce_histdiscr.png"), width = 600)
plot(amces, group = "histdis", vline = 0.0)
dev.off()

# Figure A4: MM for Historical discrimination all attributes
x <- cj(na.omit(afro), f1, id = id, estimate = "mm", by = ~histdis, 
        feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))

png(file=paste0(images,"/mm_histdiscr.png"), width = 600)
plot(x, group = "histdis", vline = 0.5)
dev.off()

# Figure A5: See Fig2a

# Figure A6: AMCEs for for Linked Fate all attributes
amces <- cj(na.omit(afro), f1, id = ~id, estimate = "amce", by = ~linkfate, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))
png(file=paste0(images,"/amce_linkedfate.png"), width = 600)
plot(amces, group = "linkfate", vline = 0.0)
dev.off()

# Figure A7: MM for Linked Fate all attributes
x <- cj(na.omit(afro), f1, id = id, estimate = "mm", by = ~linkfate, 
        feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))
png(file=paste0(images,"/mm_linkedfate.png"), width = 600)
plot(x, group = "linkfate", vline = 0.5)
dev.off()

# Figure A8: See Fig2b

# Figure A9: AMCEs for for Afrocolombians treated worse all attributes
amces <- cj(na.omit(afro), f1, id = ~id, estimate = "amce", by = ~blvswt, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))
png(file=paste0(images,"/amce_afroworse.png"), width = 600)
plot(amces, group = "blvswt", vline = 0.0)
dev.off()

# Figure A10: MM for Afrocolombians treated worse all attributes
x <- cj(na.omit(afro), f1, id = id, estimate = "mm", by = ~blvswt, 
        feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))

png(file=paste0(images,"/mm_afroworse.png"), width = 600)
plot(x, group = "blvswt", vline = 0.5)
dev.off()

# Figure A11: See Fig2c

# Figure A12: AMCEs for Membership of an Ethnic Organization all attributes
amces <- cj(na.omit(afro), f1, id = ~id, estimate = "amce", by = ~ethorg, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))
png(file=paste0(images,"/amce_ethorg.png"), width = 600)
plot(amces, group = "ethorg", vline = 0.0)
dev.off()

# Figure A13: MM for Membership of an Ethnic Organization all attributes
x <- cj(na.omit(afro), f1, id = id, estimate = "mm", by = ~ethorg, 
        feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))
png(file=paste0(images,"/mm_ethorg.png"), width = 600)
plot(x, group = "ethorg", vline = 0.5)
dev.off()

# Figure A14: See Fig3a

# Figure A15: AMCEs for Membership of a Community Council all attributes
amces <- cj(na.omit(afro), f1, id = ~id, estimate = "amce", by = ~conscom, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))
png(file=paste0(images,"/amce_conscom.png"), width = 600)
plot(amces, group = "conscom", vline = 0.0)
dev.off()


# Figure A16: MM for Membership of a Community Council all attributes
x <- cj(na.omit(afro), f1, id = id, estimate = "mm", by = ~conscom, 
        feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))
png(file=paste0(images,"/mm_conscom.png"), width = 600)
plot(x, group = "conscom", vline = 0.5)
dev.off()

# Figure A17: See Fig3b

# Figure A18: AMCEs for Racial Discrimination full results
amces <- cj(na.omit(afro), f1, id = ~id, estimate = "amce", by = ~racdisc, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))
png(file=paste0(images,"/amce_racdisc.png"), width = 600)
plot(amces, group = "racdisc", vline = 0.0)
dev.off()

# Figure A19: MM for Racial Discrimination full results
x <- cj(na.omit(afro), f1, id = id, estimate = "mm", by = ~racdisc, 
        feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))

png(file=paste0(images,"/mm_racdisc.png"), width = 600)
plot(x, group = "racdisc", vline = 0.5)
dev.off()

# Figure A20: Differences for Racial Discrimination full results
diff_mms <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~racdisc)
diff_amces_2 <- cj(na.omit(afro), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~racdisc, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_racdisc.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

# Figure A21: AMCEs for Importance of Self-identification full results
amces <- cj(na.omit(afro), f1, id = ~id, estimate = "amce", by = ~selfid, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))
png(file=paste0(images,"/amce_selfid.png"), width = 600)
plot(amces, group = "selfid", vline = 0.0)
dev.off()

# Figure A22: MM for Importance of Self-identification full results
x <- cj(na.omit(afro), f1, id = id, estimate = "mm", by = ~selfid,
        feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))

png(file=paste0(images,"/mm_selfid.png"), width = 600)
plot(x, group = "selfid", vline = 0.5)
dev.off()

# Figure A23: Differences for Importance of Self-identification full results
diff_mms <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~selfid)
diff_amces_2 <- cj(na.omit(afro), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~selfid, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_selfid.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) +
  geom_point() +
  geom_pointrange(aes(xmin = lower, xmax = upper)) +
  geom_vline(xintercept = 0, lty = 2) +
  facet_wrap(~Estimate, ncol = 2L) +
  xlab("Estimated Difference") +
  ylab("") +
  theme(text=element_text(size=30))
dev.off()

```
# Figures A24 - A31: Interaction Effects
```{r figs_interact, warning=FALSE}
# Figure A24: Historical Discrimination and Membership to Ethnic Organizations
# Marginal Means just for features of interest
mm_hdeo <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~histdis + ethorg)
# Graphing the quantities
png(file=paste0(images,"/int_mm_hdeo.png"), width = 1100)
ggplot(as.data.frame(mm_hdeo), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0.5, lty = 2) + 
  facet_wrap(histdis ~ ethorg) + 
  xlab("Marginal Mean") + 
  ylab("") + 
  theme(text=element_text(size=20))
dev.off()

# Figure A258: Historical Discrimination and Membership to Community Councils
mm_hdcc <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~histdis + conscom)
# Graphing the quantities
png(file=paste0(images,"/int_mm_hdcc.png"), width = 1100)
ggplot(as.data.frame(mm_hdcc), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0.5, lty = 2) + 
  facet_wrap(histdis ~ conscom) + 
  xlab("Marginal Mean") + 
  ylab("") + 
  theme(text=element_text(size=20))
dev.off()

# Figure A26: MMs for the interaction between Black Treated Worse (Individual vs system) and Membership in an Ethnic Organization

mm_bweo <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~blvswt + ethorg)
# Graphing the quantities
png(file=paste0(images,"/int_mm_bweo.png"), width = 1100)
ggplot(as.data.frame(mm_bweo), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0.5, lty = 2) + 
  facet_wrap(blvswt ~ ethorg) + 
  xlab("Marginal Mean") + 
  ylab("") + 
  theme(text=element_text(size=20))
dev.off()

# Figure A27: MMs for the interaction between Black Treated Worse (Individual vs system) and Membership to a Community Council
mm_bwcc <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~blvswt + conscom)
# Graphing the quantities
png(file=paste0(images,"/int_mm_bwcc.png"), width = 1100)
ggplot(as.data.frame(mm_bwcc), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0.5, lty = 2) + 
  facet_wrap(blvswt ~ conscom) + 
  xlab("Marginal Mean") + 
  ylab("") + 
  theme(text=element_text(size=20))
dev.off()

# Figure A28: MMs for the interaction between Linked Fate (Group Identification) and Membership in an Ethnic Organization
mm_lfeo <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~linkfate + ethorg)
# Graphing the quantities
png(file=paste0(images,"/int_mm_lfeo.png"), width = 1100)
ggplot(as.data.frame(mm_lfeo), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0.5, lty = 2) + 
  facet_wrap(linkfate ~ ethorg) + 
  xlab("Marginal Mean") + 
  ylab("") + 
  theme(text=element_text(size=20))
dev.off()

# Figure A29: MMs for the interaction between Linked Fate (Group Identification) and Membership to a Community Council
mm_lfcc <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~linkfate + conscom)
# Graphing the quantities
png(file=paste0(images,"/int_mm_lfcc.png"), width = 1100)
ggplot(as.data.frame(mm_lfcc), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0.5, lty = 2) + 
  facet_wrap(linkfate ~ conscom) + 
  xlab("Marginal Mean") + 
  ylab("") + 
  theme(text=element_text(size=20))
dev.off()

# Figure A30: MMs for the interaction between the Group Consciousness Index and Membership in an Ethnic Organization
# Creating the index
afro <- afro %>% mutate(a = ifelse(histdis=="Hist.Discr:Yes", 1, 0),
                            b = ifelse(blvswt=="AfroTrtWorse:Yes", 1, 0),
                            c = ifelse(linkfate=="LinkedFate:Yes",1,0),
                            gc_index = as.factor(a + b + c))


# Calculating Marginal means
mm_gcieo <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~gc_index + ethorg)
# Graphing the quantities
png(file=paste0(images,"/int_mm_gcieo.png"), width = 1100, height = 2000)
ggplot(as.data.frame(mm_gcieo), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0.5, lty = 2) + 
  facet_wrap(gc_index ~ ethorg, ncol=2L) + 
  xlab("Marginal Mean") + 
  ylab("") + 
  theme(text=element_text(size=40))
dev.off()

# Figure A31: MMs for the interaction between the Group Consciousness Index and Membership to a Community Council
mm_gcicc <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~gc_index + conscom)
# Graphing the quantities
png(file=paste0(images,"/int_mm_gcicc.png"), width = 1100, height = 2000)
ggplot(as.data.frame(mm_gcicc), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0.5, lty = 2) + 
  facet_wrap(gc_index ~ conscom, ncol=2L) + 
  xlab("Marginal Mean") + 
  ylab("") + 
  theme(text=element_text(size=40))
dev.off()
```
# Department level analysis

```{r deptos, warning=FALSE}

# Figure A32: Differences between AMCEs and MMs by individuals living in DEpartments with a high black population

afro <- afro %>% mutate(blackdepto = as.factor(ifelse(departamento == "Valle Del Cauca" | departamento == "ChocÃ³" | departamento ==  "BolÃ­var" | departamento == "San AndrÃ©s, Providencia y Santa Catalina (ArchipiÃ©lago)", "Black Dept", "No BD")))
# Marginal Means 
diff_mms <- cj(afro, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~blackdepto)

# Individual AMCEs and AMCE differences for features of interest
diff_amces_2 <- cj(na.omit(afro), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~blackdepto, feature_labels= c("FeatCirc"= "Type of District", "FeatNumcand"= "Number of Candidates","FeatExp" = "Experience","FeatClient" = "Vote Buying","FeatEth" = "Ethnicity","FeatLegis" = "Legislative Activity","FeatFam" = "Who votes?"
))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_depto.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

# Table A5: Measures of Group Consciousness in Territories with High Black population

# GC in different territories
regs <- regs %>% mutate(black_territ = ifelse(departamento == "Valle Del Cauca" | departamento == "ChocÃ³" | departamento ==  "BolÃ­var" | departamento == "San AndrÃ©s, Providencia y Santa Catalina (ArchipiÃ©lago)",1,0))

gc_rev51 <- lm(histdis ~ black_territ + edad + female + hs + married + income, data = regs)
gc_rev51r <- coeftest(gc_rev51, vcov = vcovHC(gc_rev51, type="HC1"))
gc_rev52 <- lm(linkfate ~ black_territ + edad + female + hs + married + income, data = regs)
gc_rev52r <- coeftest(gc_rev52, vcov = vcovHC(gc_rev52, type="HC1"))
gc_rev53 <- lm(blvswt ~ black_territ + edad + female + hs + married + income, data = regs)
gc_rev53r <- coeftest(gc_rev53, vcov = vcovHC(gc_rev53, type="HC1"))

stargazer(gc_rev51r, gc_rev52r, gc_rev53r,
          column.labels = c("Historical Discrimination", "Linked Fate", "Afro treated Worse"),
          covariate.labels=c("Black Territory", "Age", "Female", "Finished high school", "Married", "Income"),
          #out=paste0(tables,"/blackdepto_regs.tex"),
          type = "text")
```


# Robustness tests i: Attention Check

```{r robustness_attncheck, warning=FALSE}
##### Attention Checks
# Figure A33: Robustness checks: Overall Conjoint Results
amces2 <- cj(att_check, f1, id = ~id, feature_labels= c("FeatCirc"= "Type of District",
                            "FeatNumcand"= "Number of Candidates",
                            "FeatExp" = "Experience",
                            "FeatClient" = "Vote Buying",
                            "FeatEth" = "Ethnicity",
                            "FeatLegis" = "Legislative Activity",
                            "FeatFam" = "Who votes?"
                            ))
plot(amces2)

# Figure A34: Robustness check: Marginal Means for Main Analysis
png(file=paste0(images,"/mm_total_AC.png"), width = 600)
plot(mm(att_check, f1, id = ~id), vline = 0.5)
dev.off()

# Figure A35: Robustness checks for Historical Discrimination (Polar Power)
# CAMCE
amces <- cj(na.omit(att_check), f1, id = ~id, estimate = "amce", by = ~histdis, 
            feature_labels= c("FeatCirc"= "Type of District",
                              "FeatNumcand"= "Number of Candidates",
                              "FeatExp" = "Experience",
                              "FeatClient" = "Vote Buying",
                              "FeatEth" = "Ethnicity",
                              "FeatLegis" = "Legislative Activity",
                              "FeatFam" = "Who votes?"
))

png(file=paste0(images,"/amce_histdiscr_AC.png"), width = 600)
plot(amces, group = "histdis", vline = 0.0)
dev.off()

# MM
mm_by <- cj(att_check, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~histdis)
png(file=paste0(images,"/mm_histdiscr_AC.png"), width = 600)
plot(mm_by, group = "histdis", vline = 0.5)
dev.off()

# Differences
diff_mms <- cj(att_check, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~histdis)
diff_amces_2 <- cj(na.omit(att_check), chosen_you ~ FeatCirc, id = ~id, 
                   estimate = "amce_diff", by = ~histdis, 
                   feature_labels= c("FeatCirc"= "Type of District",
                                     "FeatNumcand"= "Number of Candidates",
                                     "FeatExp" = "Experience",
                                     "FeatClient" = "Vote Buying",
                                     "FeatEth" = "Ethnicity",
                                     "FeatLegis" = "Legislative Activity",
                                     "FeatFam" = "Who votes?"))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_histdiscr_AC.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()


## Figure A36: Robustness checks for Linked Fate (Group Identification)
# (a) CAMCE
diff_mms <- cj(att_check, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~linkfate)

png(file=paste0(images,"/amce_linkedfate_AC.png"), width = 600)
plot(diff_mms, group = "linkfate", vline = 0.0)
dev.off()

# (b) MM
mm_by <- cj(att_check, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~linkfate)
plot(mm_by, group = "linkfate", vline = 0.5)

# (c) DIFF
diff_mms <- cj(att_check, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~linkfate)
diff_amces_2 <- cj(na.omit(att_check), chosen_you ~ FeatCirc, id = ~id, 
                   estimate = "amce_diff", by = ~linkfate, 
                   feature_labels= c("FeatCirc"= "Type of District",
                                     "FeatNumcand"= "Number of Candidates",
                                     "FeatExp" = "Experience",
                                     "FeatClient" = "Vote Buying",
                                     "FeatEth" = "Ethnicity",
                                     "FeatLegis" = "Legislative Activity",
                                     "FeatFam" = "Who votes?"))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_linkedfate_AC.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

## Figure A37: Robustness checks for Afro-treated worse (Individual vs System)
# (a) CAMCE
amces <- cj(na.omit(att_check), f1, id = ~id, estimate = "amce", by = ~blvswt, feature_labels= c("FeatCirc"= "Type of District",
                                                                                            "FeatNumcand"= "Number of Candidates",
                                                                                            "FeatExp" = "Experience",
                                                                                            "FeatClient" = "Vote Buying",
                                                                                            "FeatEth" = "Ethnicity",
                                                                                            "FeatLegis" = "Legislative Activity",
                                                                                            "FeatFam" = "Who votes?"
))
png(file=paste0(images,"/amce_afroworse_AC.png"), width = 600)
plot(amces, group = "blvswt", vline = 0.0)
dev.off()

# (b) MM
mm_by <- cj(att_check, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~blvswt)
plot(mm_by, group = "blvswt", vline = 0.5)

# (c) DIFF
diff_mms <- cj(att_check, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~blvswt)
diff_amces_2 <- cj(na.omit(att_check), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~blvswt, 
                   feature_labels= c("FeatCirc"= "Type of District",
                                     "FeatNumcand"= "Number of Candidates",
                                     "FeatExp" = "Experience",
                                     "FeatClient" = "Vote Buying",
                                     "FeatEth" = "Ethnicity",
                                     "FeatLegis" = "Legislative Activity",
                                     "FeatFam" = "Who votes?"
))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_afroworse_AC.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

## Figure A38: Robustness checks for Ethnic Organizations
# (a) CAMCE
amces <- cj(na.omit(att_check), f1, id = ~id, estimate = "amce", by = ~ethorg, 
            feature_labels= c("FeatCirc"= "Type of District",
                              "FeatNumcand"= "Number of Candidates",
                              "FeatExp" = "Experience",
                              "FeatClient" = "Vote Buying",
                              "FeatEth" = "Ethnicity",
                              "FeatLegis" = "Legislative Activity",
                              "FeatFam" = "Who votes?"
))

png(file=paste0(images,"/amce_ethorg_AC.png"), width = 600)
plot(amces, group = "ethorg", vline = 0.0)
dev.off()
# (b) MM
mm_by <- cj(att_check, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~ethorg)

plot(mm_by, group = "ethorg", vline = 0.5)
# (c) DIFF
diff_mms <- cj(att_check, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~ethorg)
diff_amces_2 <- cj(na.omit(att_check), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~ethorg, feature_labels= c("FeatCirc"= "Type of District",
                                                                                                                                     "FeatNumcand"= "Number of Candidates",
                                                                                                                                     "FeatExp" = "Experience",
                                                                                                                                     "FeatClient" = "Vote Buying",
                                                                                                                                     "FeatEth" = "Ethnicity",
                                                                                                                                     "FeatLegis" = "Legislative Activity",
                                                                                                                                     "FeatFam" = "Who votes?"
))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_ethorg_AC.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

## Figure A39: Robustness checks by participation on Community Councils
# (a) CAMCE
amces <- cj(na.omit(att_check), f1, id = ~id, estimate = "amce", by = ~conscom, feature_labels= c("FeatCirc"= "Type of District",
                                                                                             "FeatNumcand"= "Number of Candidates",
                                                                                             "FeatExp" = "Experience",
                                                                                             "FeatClient" = "Vote Buying",
                                                                                             "FeatEth" = "Ethnicity",
                                                                                             "FeatLegis" = "Legislative Activity",
                                                                                             "FeatFam" = "Who votes?"
))
png(file=paste0(images,"/amce_conscom_AC.png"), width = 600)
plot(amces, group = "conscom", vline = 0.0)
dev.off()
# (b) MM
mm_by <- cj(att_check, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~conscom)
plot(mm_by, group = "conscom", vline = 0.5)
# (c) DIFF
diff_mms <- cj(att_check, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~conscom)
diff_amces_2 <- cj(na.omit(att_check), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~conscom, feature_labels= c("FeatCirc"= "Type of District",
                                                                                                                                      "FeatNumcand"= "Number of Candidates",
                                                                                                                                      "FeatExp" = "Experience",
                                                                                                                                      "FeatClient" = "Vote Buying",
                                                                                                                                      "FeatEth" = "Ethnicity",
                                                                                                                                      "FeatLegis" = "Legislative Activity",
                                                                                                                                      "FeatFam" = "Who votes?"
))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_conscom_AC.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()
```

# Robustness tests ii: Concentration Screening
```{r, warning=FALSE}
# Figure A40: Robustness checks for Historical Discrimination (Polar Power)
# (a) CAMCE
amces <- cj(na.omit(concent), f1, id = ~id, estimate = "amce", by = ~histdis, 
            feature_labels= c("FeatCirc"= "Type of District",
                              "FeatNumcand"= "Number of Candidates",
                              "FeatExp" = "Experience",
                              "FeatClient" = "Vote Buying",
                              "FeatEth" = "Ethnicity",
                              "FeatLegis" = "Legislative Activity",
                              "FeatFam" = "Who votes?"
))

png(file=paste0(images,"/amce_histdiscr_CO.png"), width = 600)
plot(amces, group = "histdis", vline = 0.0)
dev.off()
# (b) MM
mm_by <- cj(concent, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~histdis)
# Plot for MM for some features
png(file=paste0(images,"/mm_histdiscr_CO.png"), width = 600)
plot(mm_by, group = "histdis", vline = 0.5)
dev.off()
# (c) DIFF
diff_mms <- cj(concent, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~histdis)
diff_amces_2 <- cj(na.omit(concent), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~histdis, feature_labels= c("FeatCirc"= "Type of District",
                                                                                                                                           "FeatNumcand"= "Number of Candidates",
                                                                                                                                           "FeatExp" = "Experience",
                                                                                                                                           "FeatClient" = "Vote Buying",
                                                                                                                                           "FeatEth" = "Ethnicity",
                                                                                                                                           "FeatLegis" = "Legislative Activity",
                                                                                                                                           "FeatFam" = "Who votes?"
))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_histdiscr_CO.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

# Figure A41: Robustness checks for Linked Fate (Group Identification)
# (a) CAMCE
amces <- cj(na.omit(concent), f1, id = ~id, estimate = "amce", by = ~linkfate, feature_labels= c("FeatCirc"= "Type of District",
                                                                                                   "FeatNumcand"= "Number of Candidates",
                                                                                                   "FeatExp" = "Experience",
                                                                                                   "FeatClient" = "Vote Buying",
                                                                                                   "FeatEth" = "Ethnicity",
                                                                                                   "FeatLegis" = "Legislative Activity",
                                                                                                   "FeatFam" = "Who votes?"
))
png(file=paste0(images,"/amce_linkedfate_CO.png"), width = 600)
plot(amces, group = "linkfate", vline = 0.0)
dev.off()
# (b) MM
mm_by <- cj(concent, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~linkfate)
plot(mm_by, group = "linkfate", vline = 0.5)
# (c) DIFF
diff_mms <- cj(concent, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~linkfate)
diff_amces_2 <- cj(na.omit(concent), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~linkfate, feature_labels= c("FeatCirc"= "Type of District",
                                                                                                                                            "FeatNumcand"= "Number of Candidates",
                                                                                                                                            "FeatExp" = "Experience",
                                                                                                                                            "FeatClient" = "Vote Buying",
                                                                                                                                            "FeatEth" = "Ethnicity",
                                                                                                                                            "FeatLegis" = "Legislative Activity",
                                                                                                                                            "FeatFam" = "Who votes?"
))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_linkedfate_CO.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

# Figure A42: Robustness checks for Afro-treated worst (Individual vs System)
# (a) CAMCE
amces <- cj(na.omit(concent), f1, id = ~id, estimate = "amce", by = ~blvswt, 
            feature_labels= c("FeatCirc"= "Type of District",
                              "FeatNumcand"= "Number of Candidates",
                              "FeatExp" = "Experience",
                              "FeatClient" = "Vote Buying",
                              "FeatEth" = "Ethnicity",
                              "FeatLegis" = "Legislative Activity",
                              "FeatFam" = "Who votes?"
))

png(file=paste0(images,"/amce_afroworse_CO.png"), width = 600)
plot(amces, group = "blvswt", vline = 0.0)
dev.off()
# (b) MM
mm_by <- cj(concent, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~blvswt)
plot(mm_by, group = "blvswt", vline = 0.5)
# (c) DIFF
diff_mms <- cj(concent, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~blvswt)
diff_amces_2 <- cj(na.omit(concent), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~blvswt, feature_labels= c("FeatCirc"= "Type of District",
                                                                                                                                          "FeatNumcand"= "Number of Candidates",
                                                                                                                                          "FeatExp" = "Experience",
                                                                                                                                          "FeatClient" = "Vote Buying",
                                                                                                                                          "FeatEth" = "Ethnicity",
                                                                                                                                          "FeatLegis" = "Legislative Activity",
                                                                                                                                          "FeatFam" = "Who votes?"
))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_afroworse_CO.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

# Figure A43: Robustness checks for Ethnic Organizations
# (a) CAMCE
amces <- cj(na.omit(concent), f1, id = ~id, estimate = "amce", by = ~ethorg, feature_labels= c("FeatCirc"= "Type of District",
                                                                                                 "FeatNumcand"= "Number of Candidates",
                                                                                                 "FeatExp" = "Experience",
                                                                                                 "FeatClient" = "Vote Buying",
                                                                                                 "FeatEth" = "Ethnicity",
                                                                                                 "FeatLegis" = "Legislative Activity",
                                                                                                 "FeatFam" = "Who votes?"
))
png(file=paste0(images,"/amce_ethorg_CO.png"), width = 600)
plot(amces, group = "ethorg", vline = 0.0)
dev.off()
# (b) MM
mm_by <- cj(concent, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~ethorg)
plot(mm_by, group = "ethorg", vline = 0.5)
# (c) DIFF
diff_mms <- cj(concent, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~ethorg)
diff_amces_2 <- cj(na.omit(concent), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~ethorg, feature_labels= c("FeatCirc"= "Type of District",
                                                                                                                                          "FeatNumcand"= "Number of Candidates",
                                                                                                                                          "FeatExp" = "Experience",
                                                                                                                                          "FeatClient" = "Vote Buying",
                                                                                                                                          "FeatEth" = "Ethnicity",
                                                                                                                                          "FeatLegis" = "Legislative Activity",
                                                                                                                                          "FeatFam" = "Who votes?"
))

# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_ethorg_CO.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()

# Figure A43: Robustness checks for Ethnic Organizations
# (a) CAMCE
amces <- cj(na.omit(concent), f1, id = ~id, estimate = "amce", by = ~conscom, feature_labels= c("FeatCirc"= "Type of District",
                                                                                                  "FeatNumcand"= "Number of Candidates",
                                                                                                  "FeatExp" = "Experience",
                                                                                                  "FeatClient" = "Vote Buying",
                                                                                                  "FeatEth" = "Ethnicity",
                                                                                                  "FeatLegis" = "Legislative Activity",
                                                                                                  "FeatFam" = "Who votes?"
))
png(file=paste0(images,"/amce_conscom_CO.png"), width = 600)
plot(amces, group = "conscom", vline = 0.0)
dev.off()
# (b) MM
mm_by <- cj(concent, chosen_you ~ FeatCirc, id = id, estimate = "mm", by = ~conscom)
plot(mm_by, group = "conscom", vline = 0.5)
# (c) DIFF
diff_mms <- cj(concent, chosen_you ~ FeatCirc, id = id, estimate = "mm_diff", by = ~conscom)
diff_amces_2 <- cj(na.omit(concent), chosen_you ~ FeatCirc, id = ~id, estimate = "amce_diff", by = ~conscom, feature_labels= c("FeatCirc"= "Type of District",
                                                                                                                                           "FeatNumcand"= "Number of Candidates",
                                                                                                                                           "FeatExp" = "Experience",
                                                                                                                                           "FeatClient" = "Vote Buying",
                                                                                                                                           "FeatEth" = "Ethnicity",
                                                                                                                                           "FeatLegis" = "Legislative Activity",
                                                                                                                                           "FeatFam" = "Who votes?"
))


# Creates a plot with two graphs the AMCE diff and the MM diff
diff_amces_2$Estimate <- "AMCE Difference"
diff_mms$Estimate <- "MM Difference"

# create difference plot
png(file=paste0(images,"/diffplot_conscom_CO.png"), width = 1100)
ggplot(as.data.frame(rbind(diff_amces_2, diff_mms)), aes(x=estimate, y=level)) + 
  geom_point() + 
  geom_pointrange(aes(xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = 0, lty = 2) + 
  facet_wrap(~Estimate, ncol = 2L) + 
  xlab("Estimated Difference") + 
  ylab("") + 
  theme(text=element_text(size=30))
dev.off()
```


